#region LicenseHeader

// Copyright 2012 The Trustees of Indiana University.  All rights reserved.
// 
// The Indiana University licenses this file to you under the Apache License, 
// Version 2.0 (the "License"); you may not use this file except in compliance 
// with the License.  You may obtain a copy of the License at 
// 
// http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software 
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
//  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
// See the License for the specific language governing permissions and 
// limitations under the License.
// 
// @author Thilina Gunarathne (tgunarat@indiana.edu)

#endregion

#region

using System;
using System.Text;

#endregion

namespace NAligner
{
    /// <summary>
    ///   Holds the output of a pairwise sequences alignment.
    /// </summary>
    [Serializable]
    public class PairwiseAlignment
    {
        #region Members

        /// <summary>
        ///   Gap character
        /// </summary>
        public const char GAP = '-';

        /// <summary>
        ///   Default name for sequence #1
        /// </summary>
        private const String DEFAULT_SEQUENCE1_NAME = "jaligner_1";

        /// <summary>
        ///   Default name for sequence #2
        /// </summary>
        private const String DEFAULT_SEQUENCE2_NAME = "jaligner_2";

        /// <summary>
        ///   Gap extend cost
        /// </summary>
        private float extend;

        /// <summary>
        ///   Count of gap locations
        /// </summary>
        private int gaps;

        /// <summary>
        ///   Count of identical locations
        /// </summary>
        private int identity;

        /// <summary>
        ///   Markup line
        /// </summary>
        private char[] markupLine;

        /// <summary>
        ///   Scoring matrix
        /// </summary>
        private string matrix;

        /// <summary>
        ///   Name of sequence #1
        /// </summary>
        private String name1;

        /// <summary>
        ///   Name of sequence #2
        /// </summary>
        private String name2;

        /// <summary>
        ///   Gap open cost
        /// </summary>
        private float open;

        /// <summary>
        ///   Alignment score
        /// </summary>
        private float score;

        /// <summary>
        ///   Aligned sequence #1
        /// </summary>
        private char[] sequence1;

        /// <summary>
        ///   Aligned sequence #2
        /// </summary>
        private char[] sequence2;

        /// <summary>
        ///   Count of similar locations
        /// </summary>
        private int similarity;

        /// <summary>
        ///   Alignment start location in sequence #1
        /// </summary>
        private int start1;

        /// <summary>
        ///   Alignment start location in sequence #2
        /// </summary>
        private int start2;

        #endregion

        /// <returns> Returns the extend. </returns>
        public float Extend
        {
            get { return extend; }
            set { extend = value; }
        }

        /// <returns> Returns the matrix. </returns>
        public string Matrix
        {
            get { return matrix; }
            set { matrix = value; }
        }

        /// <returns> Returns the name1. </returns>
        public string Name1
        {
            get { return name1 == null || name1.Length == 0 ? DEFAULT_SEQUENCE1_NAME : name1; }
            set { name1 = value; }
        }

        /// <returns> Returns the name2. </returns>
        public string Name2
        {
            get { return name2 == null || name2.Length == 0 ? DEFAULT_SEQUENCE2_NAME : name2; }

            set { name2 = value; }
        }

        /// <returns> Returns the open. </returns>
        public float Open
        {
            get { return open; }
            set { open = value; }
        }

        /// <returns> Returns the score. </returns>
        public float Score
        {
            get { return score; }
            set { score = value; }
        }

        /// <returns> Returns the sequence1. </returns>
        public char[] Sequence1
        {
            get { return sequence1; }
            set { sequence1 = value; }
        }

        /// <returns> Returns the sequence2. </returns>
        public char[] Sequence2
        {
            get { return sequence2; }
            set { sequence2 = value; }
        }

        /// <returns> Returns the start1. </returns>
        public int Start1
        {
            get { return start1; }
            set { start1 = value; }
        }

        /// <returns> Returns the start2. </returns>
        public int Start2
        {
            get { return start2; }
            set { start2 = value; }
        }

        /// <returns> Returns the gaps. </returns>
        public int Gaps
        {
            get { return gaps; }
            set { gaps = value; }
        }

        /// <returns> Returns the identity. </returns>
        public int Identity
        {
            get { return identity; }
            set { identity = value; }
        }

        /// <returns> Returns the markupLine. </returns>
        public char[] MarkupLine
        {
            get { return markupLine; }
            set { markupLine = value; }
        }

        /// <returns> Returns the similarity. </returns>
        public int Similarity
        {
            get { return similarity; }
            set { similarity = value; }
        }

        public float PercentIdentity
        {
            get { return (Identity/(Sequence1.Length*1.0f)); }
        }

        public float PercentSimilarity
        {
            get { return (Similarity/(Sequence1.Length*1.0f)); }
        }

        /// <summary>
        ///   Returns a summary for alignment
        /// </summary>
        public string Summary
        {
            get
            {
                StringBuilder buffer = new StringBuilder();
                buffer.Append("Sequence #1: " + Name1);
                buffer.AppendLine();
                buffer.Append("Sequence #2: " + Name2);
                buffer.AppendLine();
                //buffer.Append("Matrix: " + (matrix.Id == null ? "" : matrix.Id));
                //buffer.AppendLine();
                //buffer.Append("Gap open: " + open);
                //buffer.AppendLine();
                //buffer.Append("Gap extend: " + extend);
                //buffer.AppendLine();
                buffer.Append("Identity: " + identity + "/" + Sequence1.Length + " (" +
                              (identity/(Sequence1.Length*1.0f)).ToString("F2") + ")");
                buffer.AppendLine();
                buffer.Append("Length: " + Sequence1.Length);
                buffer.AppendLine();
                //buffer.Append("Similarity: " + similarity + "/" + Sequence1.Length + " (" + (similarity / (Sequence1.Length * 1.0f)).ToString("F2") + ")");
                //buffer.AppendLine();
                //buffer.Append("Gaps: " + gaps + "/" + Sequence1.Length + " (" + (gaps / (Sequence1.Length * 1.0f)).ToString("F2") + ")");
                //buffer.AppendLine();
                //buffer.Append("Score: " + score.ToString("F2"));
                //buffer.AppendLine();
                return buffer.ToString();
            }
        }
    }
}